'use strict';
/**
 * @name 需求详情页面Controller
 * @author zyf
 */
angular.module('metalabPartnerApp')
    .controller('TalentDetailCtrl', ['$scope', '$location', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaSEO'
        , 'talent', 'project', 'patent', 'paper'
        , function ($scope, $location, $stateParams, MetaResources, MetaRemind, MetaSEO, talent, project, patent, paper) {
            var favoriteObject;

            /**
             * 从resolve里获取talent数据
             * @type {talent|*}
             */
            $scope.talent = talent;

            $scope.projectTotal = project.headers['x-count'];
            $scope.patentTotal = patent.headers['x-count'];
            $scope.paperTotal = paper.headers['x-count'];

            $scope.talent.templateUrl = 'myPopoverTemplate.html';
            $scope.hrefSrc = $location.absUrl();

            /**
             * 获取researchArea
             */
            MetaResources.researchArea.query({talentId: talent.id}, function (data) {
                $scope.researchAreas = data;
                HasFavorited();
            }, function (error) {
                MetaRemind.error(error);
            });

            /**
             * 推荐talent
             */
            if (talent.keywords) {
                $scope.recommendPromise = MetaResources.search.website({
                        '_embed': 'user',
                        "_range": "1_10",
                        "online": "true",
                        type: "talent",
                        searchText: talent.keywords ? talent.keywords.join(' ') : ''
                    }, function (data) {
                        $scope.recommendData = data;
                    },
                    function (error) {
                        MetaRemind.serverError(error);
                    }).$promise;
            } else {
                $scope.recommendPromise = MetaResources.talent.query({
                    '_embed': 'user',
                    'online': 'true',
                    '_range': '1_10',
                    '_sort': '-rank,-pageViews'
                }, function (data) {
                    $scope.recommendData = data;
                }, function (error) {
                    MetaRemind.error(error);
                }).$promise;
            }

            var seo = {
                title: []
            };

            if (talent.organization) {
                seo.title.push(talent.organization);
            }

            if (talent.department) {
                seo.title.push(talent.department);
            }

            if (talent.user.name) {
                seo.title.push(talent.user.name);
            }

            if (talent.title) {
                seo.title.push(talent.title);
            }

            if (talent.brief) {
                seo.description = talent.brief.substring(0, 100);
            }

            if (talent.keywords) {
                seo.keywords = talent.keywords.join(',');
            }

            /**
             * SEO
             */
            MetaSEO.setSEO(seo.title.join('-'),seo.description, seo.keywords);

            /**
             * @function 是否已收藏
             */
            var HasFavorited = function () {
                MetaResources.favorite.exist({
                    'userId': '_self',
                    'targetId': $scope.talent.id
                }, function (data) {
                    $scope.isFavorited = true;
                    favoriteObject = data;
                    console.log(data);
                }, function () {
                    $scope.isFavorited = false;
                });
            };

            /**
             * @function 收藏
             */
            $scope.Favorite = function () {
                if ($scope.isFavorited && favoriteObject) {
                    MetaResources.favorite.delete({
                        'id': favoriteObject.id
                    }, function () {
                        favoriteObject = null;
                        $scope.isFavorited = false;
                        MetaRemind.success('您已经取消收藏该人才');
                    }, function () {
                        MetaRemind.error('对不起，取消收藏失败，请稍后再试');
                    });
                } else {
                    MetaResources.favorite.add({
                        userId: '_self',
                        'targetId': $scope.talent.id,
                        'targetType': 'Talent'
                    }, null, function (success) {
                        favoriteObject = success;
                        $scope.isFavorited = true;
                        MetaRemind.success("收藏成功！");
                    }, function (err) {
                        console.log(err);
                        $scope.isFavorited = false;
                        MetaRemind.error("对不起，收藏失败！请稍后再试……");
                    });
                }
            }
        }]);
